/*   
 * Copyright 2009-2010 Rob Schoening
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *    http://www.apache.org/licenses/LICENSE-2.0
 *    
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.beaconframework.ext.rs;

import java.util.Map;

import javax.ws.rs.core.MediaType;

import org.beaconframework.vaadin.data.QueryTemplate;
import org.beaconframework.vaadin.util.VaadinContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public abstract class RestQueryTemplate<T> extends QueryTemplate<T> {

    private Logger log = LoggerFactory.getLogger(RestQueryTemplate.class);
    private MediaType[] acceptContentTypes;

    private Class<? extends Object> responseEntityType;

    private WebResourceFactory webResourceFactory = null;

    private String webResourceFactoryBeanName = null;


    public WebResourceFactory getWebResourceFactory() {
        if (webResourceFactory != null) {
            return webResourceFactory;
        } else if (webResourceFactoryBeanName != null) {
            return (WebResourceFactory) VaadinContext.getApplicationContext()
                    .getBean(getWebResourceFactoryBeanName(),
                            WebResourceFactory.class);

        }
        return null;
    }


    public void setWebResourceFactory(WebResourceFactory WebResource) {
        this.webResourceFactory = webResourceFactory;
    }


    public abstract WebResource constructQuery(Map<String, Object> arg,
            WebResource wr);


    @Override
    public Object doExecuteQuery(Map<String, Object> arg) throws Exception {

        WebResource wr = getWebResourceFactory().createWebResource();
        wr = constructQuery(arg, wr);
        log.debug("GET {}", wr.getURI());
        for (int i = 0; acceptContentTypes != null
                && i < acceptContentTypes.length; i++) {
            log.debug("Accepting: " + acceptContentTypes[i]);
        }
        log.debug("As: " + getResponseEntityType());
        Object responseObj = wr.accept(acceptContentTypes).get(
                getResponseEntityType());
        return responseObj;
    }


    public MediaType[] getAcceptedMediaTypes() {
        return acceptContentTypes;
    }


    public void setAcceptedMediaTypes(MediaType... types) {
        this.acceptContentTypes = types;
    }


    public Class<? extends Object> getResponseEntityType() {
        return responseEntityType;
    }


    public void setResponseEntityType(Class<? extends Object> c) {
        this.responseEntityType = c;
    }


    public String getWebResourceFactoryBeanName() {
        return webResourceFactoryBeanName;
    }


    public void setWebResourceFactoryBeanName(String webResourceFactoryBeanName) {
        this.webResourceFactoryBeanName = webResourceFactoryBeanName;
    }

}
